﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ImageRecognition2.CustomControls;

using LowLevelGraphics.Filter;
using LowLevelGraphics.Types.Interval;

namespace ImageRecognition2.Forms
{
    /// <summary>
    /// Multi Threshold
    /// </summary>
    public partial class FormMultiThreshold : FormSingleLayerMultipleThreshold
    {
        protected MultiThreshold m_MultiThreshold = new MultiThreshold();

        /// <summary>
        /// Default Constructor
        /// </summary>
        public FormMultiThreshold()
        {
            InitializeComponent();
            this.pictureBox1.BackgroundImageLayout = ImageLayout.Zoom;
            this.Shown += new EventHandler(FormMultiThreshold_Shown);

            this.cobMode.SelectedIndexChanged += new EventHandler(cobMode_SelectedIndexChanged);
            foreach (MULTITHRESHOLD_MODE mode in Enum.GetValues(typeof(MULTITHRESHOLD_MODE)))
            {
                this.cobMode.Items.Add(mode);
            }

            foreach(FloatInterval floatInterval in m_MultiThreshold.FloatIntervall)
            {
                Control LabelR = new Label();
                LabelR.Name = "fMin";
                LabelR.Text = "Min";
                LabelR.AutoSize = true;

                Control LabelG = new Label();
                LabelG.Name = "fMax";
                LabelG.Text = "Max";
                LabelG.AutoSize = true;

                NumericUpDown fMin = new NumericUpDown();
                fMin.Minimum = 0;
                fMin.Maximum = 255;
                fMin.Value = (decimal)floatInterval.Min;
                fMin.AutoSize = true;
                fMin.Tag = floatInterval;
                fMin.ValueChanged += new EventHandler(fMinValueChanged);

                NumericUpDown fMax = new NumericUpDown();
                fMax.Minimum = 0;
                fMax.Maximum = 255;
                fMax.Value = (decimal)floatInterval.Max;
                fMax.AutoSize = true;
                fMax.Tag = floatInterval;
                fMax.ValueChanged += new EventHandler(fMaxValueChanged);
                Panel p = new Panel();
                p.AutoSize = true;
                p.BorderStyle = BorderStyle.FixedSingle;

                customControlListBox1.AutoSizeControls = true;
                customControlListBox1.Items.Add(new ListBoxControlItem(LabelR, fMin, LabelG, fMax));
            }
        }

        void cobMode_SelectedIndexChanged(object sender, EventArgs e)
        {
            m_MultiThreshold.MODE = (MULTITHRESHOLD_MODE)cobMode.SelectedItem;
            Bitmap bitmap = (Bitmap)m_ImageOriginal.Clone();
            Execute(bitmap);
        }

        void fMinValueChanged(object sender, EventArgs e)
        {
            NumericUpDown nud = (NumericUpDown)sender;
            FloatInterval floatInterval = (FloatInterval)nud.Tag;
            floatInterval.Min = (float)nud.Value;

            Bitmap bitmap = (Bitmap)m_ImageOriginal.Clone();
            Execute(bitmap);
        }

        /// <summary>
        /// Executes filter
        /// </summary>
        private void Execute(Bitmap bitmap)
        {
            m_MultiThreshold.Execute(bitmap).Dispose();
            pictureBox1.BackgroundImage = bitmap;
            pictureBox1.Invalidate();
        }

        void fMaxValueChanged(object sender, EventArgs e)
        {
            NumericUpDown nud = (NumericUpDown)sender;
            FloatInterval floatInterval = (FloatInterval)nud.Tag;
            floatInterval.Max = (float)nud.Value;

            Bitmap bitmap = (Bitmap)m_ImageOriginal.Clone();
            Execute(bitmap);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void FormMultiThreshold_Shown(object sender, EventArgs e)
        {
            m_ImageOriginal = MainFormSingleton.ActivePictureBox.BackgroundImage;
            this.pictureBox1.BackgroundImage = m_ImageOriginal;
        }

        private void cbOK_Click(object sender, EventArgs e)
        {
            Image image = (Image)m_ImageOriginal.Clone();
            Execute((Bitmap)image);
            MainFormSingleton.ActivePictureBox.BackgroundImage = image;
        }
    }
}
